$(function () {
    $("#operating").combobox({
       onSelect:function (record) {
           if(record.value == '1'){
               batchStop();
           }else if(record.value == '2'){
               batchDelte();
           }else if(record.value == '3'){
               batchStart();
           }
           $("#operating").combobox('select',0);
       }
    });
    // 初始化下拉选框
    initDatagrid();

    $("#queryButton").click(function(){
        $("#listTable").datagrid('load',{
            queryStr:$("#queryStr").val()
        })
    });
});

// 表格内容初始化
function initDatagrid() {
    var url = $.util.getRequestJson('/json/storeManage.storeList.json');
    $('#listTable').datagrid({
        // type=0 即查询 普通公告类型数据
        url: url /*+ '?type=0'*/,
        idField:'id',
        queryParams:{
            type:0
        },
        fit: true,
        pagination: true,
        onLoadSuccess:function () {
            $('.cc').combobox({
                data: [{
                        method: 'viewStoreInfo',
                        title: '查看商户信息'
                    },{
                        method: 'modifyStoreInfo',
                        title: '修改商户信息'
                    },{
                        method: 'stopStore',
                        title: '停用商户'
                    },{
                        method: 'beginStore',
                        title: '启用商户'
                    },{
                        method: 'deleteStore',
                        title: '删除商户'
                    },{
                        method: 'resetPassword',
                        title: '重置管理员密码'
                    } //,{
                    //     method: 'stopCode',
                    //     title: '停用收分码'
                    // },{
                    //     method: 'beginCode',
                    //     title: '启用收分码'
                    // }
                    ],
                onSelect:function (record) {
                    var rowindex = $(this).attr('index');
                    if(record.method=='viewStoreInfo'){viewStoreInfo(rowindex);return}
                    if(record.method=='modifyStoreInfo'){modifyStoreInfo(rowindex);return}
                    if(record.method=='stopStore'){stopStore(rowindex);return}
                    if(record.method=='deleteStore'){deleteStore(rowindex);return}
                    if(record.method=='resetPassword'){resetPassword(rowindex);return}
                    if(record.method=='stopCode'){stopCode(rowindex);return}
                    if(record.method=='beginStore'){beginStore(rowindex);return}
                    if(record.method=='beginCode'){beginCode(rowindex);return}
                },
                valueField:'method',
                textField:'title'
            });

        },
        pageSize: 30,
        checkOnSelect: false,
        selectOnCheck: false,
        singleSelect: true,
        toolbar: '#toolbarButton',
        fitColumns: true,
        columns:[
            [
                {
                    field:"ck",
                    checkbox:"true"
                },
                {
                    field: 'name',
                    title: '商户名称',
                    align: 'center'
                },{
                    field: 'license',
                    title: '营业执照',
                    align: 'center',
                    width:10,
                    formatter:function (value, row, index) {
                        var pathName = location.pathname
                        pathName = pathName.substr(1,pathName.indexOf("/rs"));
                        return "<a href='"+pathName+value+"' target='_blank'>营业执照</a>"
                    }
                },{
                    field: 'address',
                    title: '地理位置'
                },{
                    field:'legalPerson',
                    title:'法人',
                    align: 'center',
                    width:12
                },{
                    field:'contactPerson',
                    title:'联系人',
                    align: 'center',
                    width:12
                },{
                    field:'contactPhone',
                    title:'联系人电话',
                    align: 'center',
                    width:12
                },{
                    field:'gatherAccount',
                    title:'收款账户',
                    align: 'center',
                    width:12
                },{
                    field:'openBank',
                    title:'开户网点',
                    align: 'center',
                    width:12
                },{
                    field:'accountName',
                    title:'开户人姓名',
                    align: 'center',
                    width:12
                }// ,{
                //     field:'status',
                //     title:'收分码状态',
                //     width:12,
                //     formatter:function (value, row, index) {
                //         return value == null ? "未绑定":"已绑定";
                //     }
                // }
                    ,{
                    field:'storeStatus',
                    title:'商户状态',
                    align: 'center',
                    width:10,
                    formatter:function (value,row,index) {
                        return value == 1 ? '启用':'停用';
                    }
                },{
                    field: 'context',
                    width:20,
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row,index) {
                        var aEl = '<input index="'+index+'" class="cc" name="dept" value="操作选项" obj="{1}">';
                        //格式化操作按钮
                        return $.string.format(aEl,row.id,row);
                    }
                }
            ]
        ]
    });
}

// 添加合作商户
function addStore() {
    var title = '添加合作商户';
    var url = $.util.getRootPath() + '/page/operatorManager/storeManager/editStore.html';
    // url = codeId ? url + '?id=' + codeId : url;
    var dialog = $.easyui.showDialog({
        iconCls:"",
        title: title,
        width: 700,
        height: 500,
        maximizable: true,
        href: url,
        topMost: true,
        enableSaveButton:true,
        saveButtonText:'保存',
        closeButtonText: '关闭',
        onSave:function () {
            saveStore(dialog);
        }
    });
}

// 修改商户商户信息
function modifyStoreInfo(index) {
    var id = $("#listTable").datagrid('getRows')[index].id;
    var title = '修改商户';
    var url = $.util.getRootPath() + '/page/operatorManager/storeManager/editStore.html?id='+id;
    $("#listTable").datagrid('reload');
    var dialog = $.easyui.showDialog({
        iconCls:"",
        title: title,
        width: 700,
        height: 500,
        maximizable: true,
        href: url,
        topMost: true,
        enableSaveButton:true,
        saveButtonText:'保存',
        closeButtonText: '关闭',
        onSave:function () {
            saveStore(dialog);
         }//,
        // onClose:function (dialog) {
        //     //parent.$(this).dialog('destroy');
        //     $("#listTable").datagrid('reload');
        // }
    });
}
// 查看商户商户信息
function viewStoreInfo(index) {
    var id = $("#listTable").datagrid('getRows')[index].id;
    var title = '查看商户';
    var url = $.util.getRootPath() + '/page/operatorManager/storeManager/editStore.html?view=view&id='+id;
    $("#listTable").datagrid('reload');
    var dialog = $.easyui.showDialog({
        iconCls:"",
        title: title,
        width: 700,
        height: 500,
        maximizable: true,
        href: url,
        topMost: true,
        enableSaveButton:false,
        saveButtonText:'保存',
        closeButtonText: '关闭',
        onSave:function () {
            saveStore(dialog);
         }
    });
}

// 保存商户
function saveStore(dialog) {
    var url = $.util.getRootPath() + '/storeManage/saveStore';
    var form = parent.$('#storeDetail').form('submit', {
        url: url,
        onSubmit: function () {
            var submitFlag = parent.$('#storeDetail').form('validate');
            var scope = parent.$('#sysDictionaryId').combobox('getValue');
            var pid = parent.$('#parentOrgId').combobox('getValue');
            var type = parent.$('#type').combobox('getValue');
            var attachedFile = parent.$("#attachedFile").val();
            var logoFile = parent.$("#logoFile").val();
            if(logoFile == ""){
                $.easyui.messager.show('未设置商户的logo,未设置的商户logo将使用默认logo展示');
            }
            if(pid == "" || scope == "" || type == "" || attachedFile == ""){
                submitFlag = false;
            }
            if(!submitFlag){
                parent.$.messager.alert('请先完善商户信息');
            }
            return submitFlag;
        },
        success: function (data) {
            if (data.success) {
                $('#listTable').datagrid('reload');
                dialog.dialog('close');
                $.easyui.messager.show(data.message);
            }else{
                $.easyui.messager.show(data.message+"<br/>请修改相关数据并重新保存");
            }
        }
    });
}

// 停用商户
function stopStore(index) {
    var id = $("#listTable").datagrid('getRows')[index].id;
    $.messager.confirm('停用商户','确定要停用选中的商户吗',function (r) {
        if(r){
             $.util.get($.util.getRequestJson('/storeManage/updateStore'),{ids:id,status:0},function(data){
                 $("#listTable").datagrid('reload');
            });
        }
            $("#listTable").datagrid('reload');
    })
}

// 删除商户
function deleteStore(index) {
    var id = $("#listTable").datagrid('getRows')[index].id;
    $.messager.confirm('删除商户','确定要删除选中的商户吗',function (r) {
        if(r){
            $.util.get($.util.getRequestJson('/storeManage/updateStore'),{ids:id,status:-1},function(data){
                $("#listTable").datagrid('reload');
            });
        }
            $("#listTable").datagrid('reload');
    })
}

// 重置密码
function resetPassword(index) {
    var id = $("#listTable").datagrid('getRows')[index].id;
    $.messager.confirm('重置密码','确定要重置管理员密码？',function (r) {
        if(r){
            $.util.get($.util.getRequestJson('/storeManage/resetAdminPassword'),{id:id},function(data){
                $("#listTable").datagrid('reload');
            });
        }
    })
    $("#listTable").datagrid('reload');
}

// 停用收分码
function stopCode(index) {
    var id = $("#listTable").datagrid('getRows')[index].id;
    //$("#listTable").datagrid('getRows')[index].status == null;
    $.messager.confirm('停用收分码','确定要停用该商户的收分码吗？',function (r) {
        if(r){
            $.util.get($.util.getRequestJson('/consumeManage/updateCode'),{storeId:id},function(data){
                $("#listTable").datagrid('reload');
            });
            $("#listTable").datagrid('reload');
        }
    })
    $("#listTable").datagrid('reload');
}
function beginStore(index) {
    //alert('启用商户')
    var id = $("#listTable").datagrid('getRows')[index].id;
    $.messager.confirm('确定要启用该商户？',function (r) {
        if(r){
            $.util.get($.util.getRequestJson('/storeManage/updateStore'),{ids:id,status:1},function(data){
                $("#listTable").datagrid('reload');
            });
        }
        $("#listTable").datagrid('reload');
    })
}
// 启用收分码
function beginCode(index) {
    var id = $("#listTable").datagrid('getRows')[index].codeId;
    if(id){
        $.messager.confirm('启用收分码','确定要启用该商户的收分码吗？',function (r) {
            if(r){
                $.util.get($.util.getRequestJson('/consumeManage/useCode'),{codeId:id},function(data){
                    $("#listTable").datagrid('reload');
                });
                $("#listTable").datagrid('reload');
            }
        })
    }
    $("#listTable").datagrid('reload');
}
// 批量删除商户
function batchDelte() {
    var checkeds = $("#listTable").datagrid('getChecked');
    if(checkeds.length == 0){
        $.messager.alert('提示','未选择要删除的商户项');
        return ;
    }
    var ids = "";
    for(var i = 0; i < checkeds.length;i++){
        if(i == checkeds.length-1){
            ids += checkeds[i].id;
        }else{
            ids += checkeds[i].id + "_";
        }
    }
    $.messager.confirm('批量删除商户','已选择<b>'+checkeds.length+'</b>个商户项，确定要删除这些商户吗？',function (r) {
        if(r){
            $.util.get($.util.getRequestJson('/storeManage/updateStore'),{ids:ids,status:-1},function(data){
                $("#listTable").datagrid('reload');
            });
            $("#listTable").datagrid('reload');
            $("#listTable").datagrid('clearChecked');
        }
    })
}

// 批量停用商户
function batchStop() {
    var checkeds = $("#listTable").datagrid('getChecked');
    if(checkeds.length == 0){
        $.messager.alert('提示','未选择要停用的商户项');
        return ;
    }
    var ids = "";
    for(var i = 0; i < checkeds.length;i++){
        if(i == checkeds.length-1){
            ids += checkeds[i].id;
        }else{
            ids += checkeds[i].id + "_";
        }
    }
    $.messager.confirm('批量停用商户','已选择<b>'+checkeds.length+'</b>个商户项，确定要停用这些商户吗？',function (r) {
        if(r){
            $.util.get($.util.getRequestJson('/storeManage/updateStore'),{ids:ids,status:0},function(data){
                $("#listTable").datagrid('reload');
            });
            $("#listTable").datagrid('reload');
            $("#listTable").datagrid('clearChecked');
        }
    })
}
// 批量启用商户
function batchStart() {
    var checkeds = $("#listTable").datagrid('getChecked');
    if(checkeds.length == 0){
        $.messager.alert('提示','未选择要启用的商户项');
        return ;
    }
    var ids = "";
    for(var i = 0; i < checkeds.length;i++){
        if(i == checkeds.length-1){
            ids += checkeds[i].id;
        }else{
            ids += checkeds[i].id + "_";
        }
    }
    $.messager.confirm('批量启用商户','已选择<b>'+checkeds.length+'</b>个商户项，确定要启用这些商户吗？',function (r) {
        if(r){
            $.util.get($.util.getRequestJson('/storeManage/updateStore'),{ids:ids,status:1},function(data){
                $("#listTable").datagrid('reload');
            });
            $("#listTable").datagrid('reload');
            $("#listTable").datagrid('clearChecked');
        }
    })
}
